var Sai = function () {
    /*this.layout = new Sai.Layout();*/
    $(".form-table > table").tablesorter();
}
Sai.fn = Sai.prototype

Sai.Layout = function () {
    this.topMenu    = $("#top-menu");
    this.leftMenu   = $("#left-menu");
    this.module     = $("#module");
    
    $(window).resize($.proxy(this.onWindowResize, this));
    $(window).load($.proxy(this.onWindowResize, this));
    this.onWindowResize();
}
Sai.Layout.fn = Sai.Layout.prototype

Sai.Layout.fn.onWindowResize = function (event){
    var height = $(window).height() - this.topMenu.height() - 3;
    var width = $(window).width() - 230;
    
    this.leftMenu.height(height);
    this.module.height(height);
    this.module.width(width);
}

Sai.FormTables = function () {
    this.tableItemsMenu = $("#table-item-menu");
    $("div.form-table tbody > tr").click($.proxy(this.onRowClick, this));
}
Sai.FormTables.fn = Sai.FormTables.prototype;

Sai.FormTables.fn.onRowClick = function (event){
    console.log(event.currentTarget);
    var tr = $(event.currentTarget);
    var tbody = tr.parent();
        tbody.find("tr.selected").removeClass("selected");
    
    tr.addClass("selected");
    
    var variables = tr.data("variables");
    
    $.ajax({
        type: "POST",
        url: Sai.HOST + "modules/" + Sai.MODULE + "/actions/list/scripts/table-item-menu.php",
        data: {
            variables:variables
        },
        success: $.proxy(this.onSuccess, this)
    });
}

Sai.FormTables.fn.onSuccess = function (data){
    console.log("onSuccess");
    console.log(data);
    this.tableItemsMenu.empty();
    this.tableItemsMenu.append(data);
};

var SAI;

$(document).ready(function() {
    SAI = new Sai();
    
    new Sai.FormTables();
});